package com.google.code.shardbatis.converter;

import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.update.Update;

public class UpdateSqlConverter extends AbstractSqlConverter {
	
	@Override
	public String getOriginalTableName(Statement statement) {
		
		if (!(statement instanceof Update)) {
			throw new IllegalArgumentException("The argument statement must is instance of Update.");
		}

		Update update = (Update) statement;
		
		if(update.getTables().isEmpty()){
			throw new IllegalArgumentException("The table name is unknow.");
		}
		
		return update.getTables().get(0).getName();
	}

	@Override
	protected Statement doConvert(Statement statement, String tableName) {
		if (!(statement instanceof Update)) {
			throw new IllegalArgumentException("The argument statement must is instance of Update.");
		}

		Update update = (Update) statement;
		
		if(update.getTables().isEmpty()){
			throw new IllegalArgumentException("The table name is unknow.");
		}
		
		update.getTables().get(0).setName(tableName);
		
		return update;
	}
	
}